package leesoft.intg.plugins;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import leesoft.configuration.core.DBToolKit;
import leesoft.configuration.interfaces.AfterPlugin;
import leesoft.configuration.interfaces.IRecordSetMapHandler;

public class FieldsMapingFromDB extends AfterPlugin implements  IRecordSetMapHandler{
	Map innerMap=new HashMap<String,String>();
	@Override
	public void beforeExecute(){
		String driverName=(String)parameters.get("db_driver");
		String connStr=(String)parameters.get("db_conn");
		String username=(String)parameters.get("db_username");
		String password=(String)parameters.get("db_password");
		String query=(String)parameters.get("query");
		
		
		
		DBToolKit dbk=new DBToolKit();
		dbk.open(driverName,connStr,username,password);
		dbk.executeOn(query,this);
		dbk.close();
	}
	public void execute(Map rsMap) {
		String sourceField=(String)parameters.get("source_field");
		String targetField=(String)parameters.get("target_field");
		String value=(String)rsMap.get(sourceField.toLowerCase());
		if(value==null)
			value=(String)parameters.get("default_value");
		else
			value=(String)innerMap.get(value);
		rsMap.put(targetField.toLowerCase(), value);
	}
	@Override
	public boolean handle(Map rowMap) {
		String map_key=(String)parameters.get("map_key");
		String map_value=(String)parameters.get("map_value");
		innerMap.put(rowMap.get(map_key), rowMap.get(map_value));
		return true;
	}

}
